package com.jwdf.uda.ui.adapter.viewholder;

import android.databinding.DataBindingUtil;
import android.view.View;

import com.jwdf.uda.databinding.ItemQuizTitleTabBinding;
import com.jwdf.uda.event.QuizDetailEvent;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.ui.adapter.item.QuizDetailItem;
import com.jwdf.uda.utils.EventBusUtil;

/**
 * Created by Administrator on 2016/9/6.
 */
public class QuizTitleTabViewHolder extends AbsViewHolder<QuizDetailItem> {
    private ItemQuizTitleTabBinding mItemBinding;

    public QuizTitleTabViewHolder(View convertView) {
        super(convertView);
        mItemBinding = DataBindingUtil.bind(convertView);
    }

    @Override
    protected void onBindView(final QuizDetailItem item) {
        mItemBinding.tvTabComment.setText(String.valueOf(item.replyCount));
        mItemBinding.tvTabVote.setText(String.valueOf(item.voteCount));
        mItemBinding.tvSort.setText(item.sortType);
        mItemBinding.flTabVote.setVisibility(item.hasVoteOption ? View.VISIBLE : View.GONE);
        if (item.currentTab == QuizDetailItem.QuizTab.VOTE) {
            showTabVote();
        } else {
            showTabComment();
        }
        mItemBinding.flTabComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.currentTab = QuizDetailItem.QuizTab.REPLY;
                QuizDetailEvent event = new QuizDetailEvent(QuizDetailEvent.EVENT_TAB_REPLY);
                event.count = item.replyCount;
                EventBusUtil.post(event);
            }
        });
        mItemBinding.flTabVote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.currentTab = QuizDetailItem.QuizTab.VOTE;
                QuizDetailEvent event = new QuizDetailEvent(QuizDetailEvent.EVENT_TAB_VOTE);
                event.count = item.voteCount;
                EventBusUtil.post(event);
            }
        });
        mItemBinding.flDropMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.replyCount > 0) {
                    item.currentTab = QuizDetailItem.QuizTab.REPLY;
                    QuizDetailEvent event = new QuizDetailEvent(QuizDetailEvent.EVENT_SHOW_DROP_MENU);
                    event.anchor = v;
                    EventBusUtil.post(event);
                }
            }
        });
    }

    private void showTabComment() {
        mItemBinding.tvSort.setSelected(true);
        mItemBinding.tvTabComment.setSelected(true);
        mItemBinding.tvTabVote.setSelected(false);
        mItemBinding.tvVoteTitleComment.setSelected(true);
        mItemBinding.tvVoteTitleText.setSelected(false);
        mItemBinding.vLineComment.setVisibility(View.VISIBLE);
        mItemBinding.vLineVote.setVisibility(View.GONE);
        mItemBinding.flDropMenu.setVisibility(item.replyCount > 0 ? View.VISIBLE : View.GONE);
        mItemBinding.divider.setVisibility(item.replyCount > 0 ? View.VISIBLE : View.GONE);
    }

    private void showTabVote() {
        mItemBinding.tvSort.setSelected(false);
        mItemBinding.tvTabComment.setSelected(false);
        mItemBinding.tvTabVote.setSelected(true);
        mItemBinding.tvVoteTitleComment.setSelected(false);
        mItemBinding.tvVoteTitleText.setSelected(true);
        mItemBinding.vLineComment.setVisibility(View.GONE);
        mItemBinding.vLineVote.setVisibility(View.VISIBLE);
        mItemBinding.flDropMenu.setVisibility(View.GONE);
        mItemBinding.divider.setVisibility(View.GONE);
    }
}
